/*
  This file is part of an addition to the model that reads, writes, and displays information
  This particular file reads, writes, and displays race information.
*/


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "ssm.h"

//This paticular print function prints the race timing information
void printRace(Race source)
{
	printf("time_start = %f\n", source.time_start);
	printf("time_stop  = %f\n", source.time_stop);
}

//This function writes the race information to a file, most of the time, this file is called race.str
//returns 0 on a sucsessful write
int writeRace(char* filename, Race *source)
{
	FILE *fp;
	if((fp=fopen(filename, "wb")) == NULL) 
	{//error opening file
  		fprintf(stderr,"ERROR: can not open %s for output\n", filename);
  		exit(1);
	}
	if((fwrite(source, sizeof(Race), 1, fp)) == -1)
	{//write failed
		fprintf(stderr,"ERROR: could not write record\n");
	}
	fclose(fp);
	return 0;
}

//This function reads in race information and stores it to dest
//returns 0 on a sucsessful read 
int readRace(char* filename, Race *dest)
{
	FILE *fp;
	if((fp=fopen(filename,"rb")) == NULL) 
	{//error opening file
  		fprintf(stderr,"ERROR: can not open %s for input\n", filename);
  		exit(1);
	}
	if((fread(dest, sizeof(Race), 1, fp)) == -1)
	{//write failed
		fprintf(stderr,"ERROR: could not write record\n");
	}
	fclose(fp);
	return 0;
}
